import collections
class Solution(object):
    def groupStrings(self, words):
        def convert(word):
            val = 0
            for c in word:
                val |= (1 << (ord(c) - ord('a')))
            return val
        d = collections.defaultdict(int)
        for word in words:
            d[convert(word)] += 1
        p = {}
        c = {}
        for key in d.keys():
            p[key] = key
            c[key] = 0

        def find(x):
            if p[x] == x:
                return p[x]
            else:
                p[x] = find(p[x])
                return p[x]
        def union(x, y):
            rootx = find(x)
            rooty = find(y)
            if rootx != rooty:
                if rootx > rooty:
                    p[rootx] = rooty
                else:
                    p[rooty] = rootx

        def dfs(val):
            c[val] = 1
            for i in range(26):
                for j in range(i, 26):
                    if i == j:
                        next_val = val ^ (1 << i)
                        if next_val in c and c[next_val] == 0:
                            union(val, next_val)
                            dfs(next_val)
                    else:
                        if ((val >> i) & 1) ^ ((val >> j) & 1) == 1:
                            next_val = val ^ (1 << i)
                            next_val ^= (1 << j)
                            if next_val in c and c[next_val] == 0:
                                union(val, next_val)
                                dfs(next_val)

        for key in c:
            if c[key] == 0:
                dfs(key)
        out = collections.defaultdict(int)
        for key in p:
            rx = find(key)
            out[rx] += d[key]
        return [len(out), max(out.values())]


data = Solution()
words = ["a","b","ab","cde"]
print(data.groupStrings(words))
words = ['a', 'b']
print(data.groupStrings(words))